package com.aloda.controller.user;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.constant.ErrorCode;
import com.aloda.dto.scoringResult.BatchSResultAddDTO;
import com.aloda.dto.scoringResult.ScoringResultAddDTO;
import com.aloda.dto.scoringResult.ScoringResultEditDTO;
import com.aloda.dto.scoringResult.ScoringResultGenerateDTO;
import com.aloda.exception.BaseException;
import com.aloda.exception.WarnException;
import com.aloda.query.ScoringResultUserPageDTO;
import com.aloda.result.PageResult;
import com.aloda.result.Result;
import com.aloda.service.ScoringResultService;
import com.aloda.vo.scoringResult.ScoringResultGenerateVO;
import com.aloda.vo.scoringResult.ScoringResultInfoVO;
import com.aloda.vo.scoringResult.ScoringResultPageVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController("result-user")
@RequestMapping("/user/result")
public class ScoringResultController {

    @Resource
    private ScoringResultService scoringResultService;

    /**
     * 客户端新增评分结果
     * @param scoringResultAddDTO scoringResultAddDTO
     * @return id
     */
    @PostMapping
    public Result<Long> addResult(@RequestBody ScoringResultAddDTO scoringResultAddDTO) {
        log.info("客户端新增评分结果:{}",scoringResultAddDTO);
        if (BeanUtil.isEmpty(scoringResultAddDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(scoringResultService.addResultByUser(scoringResultAddDTO));
    }

    /**
     * 客户端分页查询评分结果
     * @param scoringResultUserPageDTO
     * @return
     */
    @PostMapping("/page")
    public Result<PageResult<ScoringResultPageVO>> pageQuery(@RequestBody ScoringResultUserPageDTO scoringResultUserPageDTO) {
        if (BeanUtil.isEmpty(scoringResultUserPageDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        PageResult<ScoringResultPageVO> pageResult = scoringResultService.pageQueryByUser(scoringResultUserPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 客户端编辑评分结果
     * @param scoringResultEditDTO
     * @return
     */
    @PutMapping
    public Result<Long> editResult(@RequestBody ScoringResultEditDTO scoringResultEditDTO) {
        log.info("客户端编辑评分结果:{}",scoringResultEditDTO);
        if (BeanUtil.isEmpty(scoringResultEditDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(scoringResultService.editResultByUser(scoringResultEditDTO));
    }

    /**
     * 客户端获取评分结果详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<ScoringResultInfoVO> getResultById(@PathVariable Long id) {
        if (id == null) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(scoringResultService.getResultById(id));
    }

    /**
     * 客户端删除评分结果
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<Long> deleteResult(@RequestBody List<Long> ids) {
        log.info("客户端删除评分结果:{}",ids);
        if (ids.isEmpty()) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(scoringResultService.deleteResultByIds(ids));
    }

    /**
     * AI生成评分结果
     * @param scoringResultGenerateDTO
     * @return
     */
    @PostMapping("/ai")
    public Result<List<ScoringResultGenerateVO>> aiGenerate(@RequestBody ScoringResultGenerateDTO scoringResultGenerateDTO) {
        log.info("客户端AI生成评分结果：{}",scoringResultGenerateDTO);
        if (BeanUtil.isEmpty(scoringResultGenerateDTO)) {
            throw new WarnException(ErrorCode.PARAMS_ERROR);
        }
        return Result.success(scoringResultService.aiGenerate(scoringResultGenerateDTO));
    }

    /**
     * 批量新增评分结果
     * @param batchSResultAddDTO
     * @return
     */
    @PostMapping("/batch")
    public Result saveGenerateResultBatch(@RequestBody BatchSResultAddDTO batchSResultAddDTO) {
        log.info("批量生成评分结果：{}",batchSResultAddDTO);
        if (BeanUtil.isEmpty(batchSResultAddDTO)) {
            return Result.error(ErrorCode.PARAMS_ERROR);
        }
        scoringResultService.saveGenerateResultBatch(batchSResultAddDTO);
        return Result.success();
    }
}
